/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.Rottenpotatoes.controller;

import java.io.Serializable;
import java.util.ArrayList;
import javax.faces.application.FacesMessage;
import javax.inject.Named;
import org.Rottenpotatoes.model.Movies;
import org.Rottenpotatoes.util.HibernateUtil;



import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.Rottenpotatoes.util.FacesUtil;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author yolanda
 */
@Named(value = "moviesBean")
@ViewScoped
public class moviesBean  extends AbstractViewScopedBean implements Serializable {
    
     //declaracion de variables
       
    private Movies movie;  //Una instancia de la clase Persona (Tabla persona)
    private ArrayList<Movies> listamovies;
    private Movies selectedmovie;

    /**
     * Creates a new instance of moviesBean
     */
    public moviesBean() {
         this.nuevo();
    }

    
    /** Muestra los datos de la BD en la tabla*/
    //metodo para listar movies
    public void mostrarmovies() {
        this.listamovies = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria cMovie = session.createCriteria(Movies.class);
        this.listamovies = (ArrayList<Movies>) cMovie.list();
        session.close();

    }
    
    @Override
    public void crear() {
        
          //guardar personas
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            
            session.saveOrUpdate(this.movie);//guardarlos
            tx.commit();//confirmar transaccion
            this.nuevo();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Informacion:", "Movie  Creada se registro satisfactoriamente"));
        } catch (HibernateException e) {
            tx.rollback();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Un error ha ocurrido:", e.getCause().getMessage()));
        } finally {
            session.close();
        }
    }

    @Override
    public void eliminar() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        Movies m = (Movies) session.load(Movies.class, this.selectedmovie.getId());
     
        try {
            tx = (Transaction) session.beginTransaction();
            session.delete(m);
            tx.commit();
            this.nuevo();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Información:", "Registro eliminado satisfactoriamente"));
        } catch (HibernateException e) {
            tx.rollback();
            FacesUtil.hibernateException(e);
        } finally {
            session.close();
        }
    }

    @Override
    public void buscar() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void modificar() {
        this.movie = this.selectedmovie;
    }

    @Override
    public void nuevo() {
        this.movie = new Movies();
        this.listamovies = new ArrayList<Movies>();
        this.selectedmovie = new Movies() ;
        this.mostrarmovies();
    }
    
    @Override
    public void cargarResgistros() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public Movies getMovie() {
        return movie;
    }

    public void setMovie(Movies movie) {
        this.movie = movie;
    }

    public ArrayList<Movies> getListamovies() {
        return listamovies;
    }

    public void setListamovies(ArrayList<Movies> listamovies) {
        this.listamovies = listamovies;
    }

    public Movies getSelectedmovie() {
        return selectedmovie;
    }

    public void setSelectedmovie(Movies selectedmovie) {
        this.selectedmovie = selectedmovie;
    }
    
    
    
    

    
}
